package com.example.spring.boot.rabbitmq.configure;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TtlQueueConfigure {


    @Bean(name = "xExchange")
    public Exchange xExchange() {

        return new DirectExchange("X");
    }


    @Bean(name = "yExchange")
    public Exchange yExchange() {

        return new DirectExchange("Y");
    }


    @Bean(name = "order")
    public TopicExchange topicExchange() {

        return new TopicExchange("order");
    }

    @Bean(name = "stockQ")
    public Queue stockQ() {
        return new Queue("stockQ");
    }


    @Bean(name = "stockBinding")
    public Binding stockBinding(@Qualifier(value = "order") TopicExchange topicExchange,
                                @Qualifier(value = "stockQ") Queue queue) {
        return BindingBuilder.bind(queue).to(topicExchange).with("order.*");
    }

    @Bean(name = "QA")
    public Queue qA() {
        Map<String, Object> args = new HashMap<>(3);
        // 声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "Y");
        // 声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        // 声明队列的 TTL
        args.put("x-message-ttl", 10000);
        return new Queue("QA", true, false, false, args);
    }


    @Bean(name = "QB")
    public Queue qB() {
        Map<String, Object> args = new HashMap<>(3);
        // 声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "Y");
        // 声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        // 声明队列的 TTL
        args.put("x-message-ttl", 40000);
        return new Queue("QB", true, false, false, args);
    }


    @Bean(name = "QD")
    public Queue qD() {
        return new Queue("QD");
    }


    //声明队列 A 绑定 X 交换机
    @Bean(name = "bindingA")
    public Binding bindingA(@Qualifier("QA") Queue queueA,
                            @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueA).to(xExchange)
                .with("XA");
    }


    //声明队列 B 绑定 X 交换机
    @Bean(name = "bindingB")
    public Binding bindingB(@Qualifier("QB") Queue queueB,
                            @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueB).to(xExchange)
                .with("XA");
    }

    // 声明死信队列 QD 绑定关系
    @Bean
    public Binding deadLetterBindingQAD(@Qualifier("QD") Queue queueD,
                                        @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }

}
